package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SelVideoAdapter;
import flc.ast.databinding.ActivitySelVideoBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class SelVideoActivity extends BaseAc<ActivitySelVideoBinding> {

    public static int type;
    private SelVideoAdapter videoAdapter;
    private String selPath;
    private int oldPosition;
    private List<String> listSel = new ArrayList<>();//选择的集合
    private String again = "";


    @Override
    protected int onCreate() {
        return R.layout.activity_sel_video;
    }

    @Override
    protected void initView() {
        mDataBinding.ivSelVideoBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvConfirm.setOnClickListener(this);

        mDataBinding.rvVideoList.setLayoutManager(new GridLayoutManager(mContext, 3));
        videoAdapter = new SelVideoAdapter();
        videoAdapter.setEdit(true);
        mDataBinding.rvVideoList.setAdapter(videoAdapter);
        videoAdapter.setOnItemClickListener(this);
        //视频拼接再次进入选择
        again = getIntent().getStringExtra(Content.Again);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvConfirm:
                if (TextUtils.isEmpty(selPath) && listSel.size() == 0) {
                    ToastUtils.showShort(R.string.please_sel_video);
                    return;
                }
                if (type == 0) {
                    VideoGifActivity.sVideoPath = selPath;
                    startActivity(VideoGifActivity.class);
                } else if (type == 1) {
                    VideoFormatActivity.sVideoPath = selPath;
                    startActivity(VideoFormatActivity.class);
                } else if (type == 2) {
                    VideoTailorActivity.videoPath = selPath;
                    startActivity(VideoTailorActivity.class);
                } else if (type == 3) {
                    if (TextUtils.isEmpty(again)) {
                        VideoSpliceActivity.listPath = listSel;
                        startActivity(VideoSpliceActivity.class);
                    } else {
                        //更换/添加视频
                        Intent intent = new Intent();
                        intent.putExtra(Content.videoPath, selPath);
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                } else if (type == 4) {
                    VideoBgActivity.videoPath = selPath;
                    startActivity(VideoBgActivity.class);
                } else if (type == 5) {
                    VideoFilterActivity.videoPath = selPath;
                    startActivity(VideoFilterActivity.class);
                } else if (type == 6) {
                    VideoRotateActivity.videoPath = selPath;
                    startActivity(VideoRotateActivity.class);
                } else if (type == 7) {
                    VideoCompressActivity.videoPath = selPath;
                    startActivity(VideoCompressActivity.class);
                }
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (type == 3) {
            if (TextUtils.isEmpty(again)) {
                if (videoAdapter.getItem(position).isChecked()) {
                    videoAdapter.getItem(position).setChecked(false);
                    listSel.remove(videoAdapter.getItem(position).getPath());
                } else {
                    videoAdapter.getItem(position).setChecked(true);
                    listSel.add(videoAdapter.getItem(position).getPath());
                }
                videoAdapter.notifyItemChanged(position);
            } else {
                videoAdapter.getItem(oldPosition).setChecked(false);
                videoAdapter.notifyItemChanged(oldPosition);
                oldPosition = position;
                videoAdapter.getItem(position).setChecked(true);
                videoAdapter.notifyItemChanged(position);
                selPath = videoAdapter.getItem(position).getPath();
            }
        } else {
            videoAdapter.getItem(oldPosition).setChecked(false);
            videoAdapter.notifyItemChanged(oldPosition);
            oldPosition = position;
            videoAdapter.getItem(position).setChecked(true);
            videoAdapter.notifyItemChanged(position);
            selPath = videoAdapter.getItem(position).getPath();
            mDataBinding.tvConfirm.setText(getString(R.string.sel_video_num, 1));
        }
    }

    @Override
    protected void initData() {
        getPermission();
        if (type == 3) {
            mDataBinding.tvConfirm.setText(getString(R.string.sel_video));
        } else {
            mDataBinding.tvConfirm.setText(getString(R.string.sel_video_num, 0));
        }
    }

    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO)
                .reqPermissionDesc(getString(R.string.get_video_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        getData();
                    }
                }).request();
    }

    //获取视频和图片数据
    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(mContext, PhotoConfig.Mode.VIDEO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    videoAdapter.setList(selectMediaEntities);
                    mDataBinding.rvVideoList.setVisibility(View.VISIBLE);
                } else {
                    mDataBinding.rvVideoList.setVisibility(View.GONE);
                }
            }
        });
    }

}